package com.chris.cc.admin.config;

import com.chris.cc.admin.notifier.DingTalkNotifier;
import de.codecentric.boot.admin.server.domain.entities.InstanceRepository;
import de.codecentric.boot.admin.server.notify.CompositeNotifier;
import de.codecentric.boot.admin.server.notify.Notifier;
import de.codecentric.boot.admin.server.notify.RemindingNotifier;
import de.codecentric.boot.admin.server.notify.filter.FilteringNotifier;
import org.springframework.beans.factory.ObjectProvider;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;

import java.time.Duration;
import java.util.Collections;
import java.util.List;

/**
 * Copyright (c) 2018-2025, chris All rights reserved.
 * <p>
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 * <p>
 * Redistributions of source code must retain the above copyright notice,
 * this list of conditions and the following disclaimer.
 * Redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the distribution.
 * Neither the name of the pig4cloud.com developer nor the names of its
 * contributors may be used to endorse or promote products derived from
 * this software without specific prior written permission.
 * Author: haoka (haokang207@126.com)
 * Date: 2018/8/7 15:33
 **/
@Configuration
@ConditionalOnProperty(name = {"spring.boot.admin.notify.dingtalk.enabled"}, havingValue = "true")
public class DingTalkNotifierConfiguration {
    @Autowired
    private InstanceRepository repository;

    @Bean
    @ConditionalOnMissingBean
    @ConfigurationProperties(prefix = "spring.boot.admin.notify.dingtalk")
    public DingTalkNotifier dingTalkNotifier() {
        return new DingTalkNotifier(repository);
    }

    @Bean
    public FilteringNotifier filteringNotifier(ObjectProvider<List<Notifier>> otherNotifiers) {
        CompositeNotifier delegate = new CompositeNotifier(otherNotifiers.getIfAvailable(Collections::emptyList));
        return new FilteringNotifier(delegate, repository);
    }

    @Primary
    @Bean(initMethod = "start", destroyMethod = "stop")
    public RemindingNotifier remindingNotifier(ObjectProvider<List<Notifier>> otherNotifiers) {
        RemindingNotifier notifier = new RemindingNotifier(filteringNotifier(otherNotifiers), repository);
        notifier.setReminderPeriod(Duration.ofMinutes(1));
        notifier.setCheckReminderInverval(Duration.ofSeconds(10));
        return notifier;
    }
}
